package com.chanceit.ssss.data;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.gl.GlobalVariable;
import org.gl.ui.manger.AjaxCallBackProxy;
import org.gl.utils.Dateutil;
import org.gl.utils.lg;
import org.json.JSONException;
import org.json.JSONObject;

import android.text.TextUtils;

import com.chanceit.ssss.common.IntentExtraDataKey;
import com.chanceit.ssss.common.WebServicUrl;

public class DataSources {

	private String TAG = "DataSources";

	
	public void getAlluser(Integer page, AjaxCallBack<?> mAjaxCallBack) {
		AjaxParams par = new AjaxParams();
		// {"buycartime":3,"maintainMileage":300,
		// "maintainTime":3,"insuTime":3 ,
		// "inspTime":3,"carName":"宝马","sex":"男","minAge":18,
		// "maxAge":18,"mileage":1000,"guaranteeKilometer":500,
		// "guaranteeTime":1,"firstMaintainKilometer":500,
		// "firstMaintainTime":1,"birthday":0[依次新增：行驶里程，质保里程，质保时间，首保里程，首保时间，生日提醒等参数]}
		JSONObject params = new JSONObject();
		try {
			params.put("buycartime", 0);
			params.put("maintainMileage", 0);
			params.put("maintainTime", 0);
			params.put("insuTime", 0);
			params.put("inspTime", 0);
			params.put("carName", "");
			params.put("sex", "");
			params.put("minAge", 0);
			params.put("maxAge", 0);
			params.put("mileage", 0);
			params.put("guaranteeKilometer", 0);
			params.put("firstMaintainTime", 0);
			params.put("birthday", 0);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// par.put("searchCode", params.toString());
		if (page != null) {
			par.put("pageNo", String.valueOf(page));
		}
		FinalHttp fh = new FinalHttp();
		fh.post(WebServicUrl.user_page, par, mAjaxCallBack);
	}

	/**
	 * user_accurate identifier 客户识别码 tel 联系电话 userName 客户名 plate 客户车牌
	 * 
	 * @param page
	 * @param mAjaxCallBack
	 * @param conditionType
	 * @param value
	 * @throws JSONException
	 */
	public void searchUser(Integer page, AjaxCallBack<?> mAjaxCallBack,
			int conditionType, String value) {
		AjaxParams par = new AjaxParams();
		JSONObject params = new JSONObject();
		try {
			switch (conditionType) {
			case SearchCarer.Condition_car_number:

				params.put("plate", value);

				break;
			case SearchCarer.Condition_Identifier:
				params.put("identifier", value);
				break;
			case SearchCarer.Condition_UserName:
				params.put("userName", value);
				break;
			case SearchCarer.Condition_Tel_number:
				params.put("tel", value);
				break;
			default:
				break;
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (page != null) {
			par.put("pageNo", String.valueOf(page));
		}
		par.put("searchCode", params.toString());
		FinalHttp fh = new FinalHttp();
		fh.post(WebServicUrl.user_page, par, mAjaxCallBack);
	}

	public void exactSearchUser(Integer page, AjaxCallBack<?> mAjaxCallBack,
			int conditionType, String value) {
		AjaxParams par = new AjaxParams();
		switch (conditionType) {
		case SearchCarer.Condition_car_number:
			par.put("plate", value);
			break;
		case SearchCarer.Condition_Identifier:
			par.put("identifier", value);
			break;
		case SearchCarer.Condition_UserName:
			par.put("userName", value);
			break;
		case SearchCarer.Condition_Tel_number:
			par.put("tel", value);
			break;
		default:
			break;
		}
		if (page != null) {
			par.put("pageNo", String.valueOf(page));
		}
		FinalHttp fh = new FinalHttp();
		fh.post(WebServicUrl.user_accurate, par, mAjaxCallBack);
	}

	public void getCarerInfo(String id, AjaxCallBack<String> ajaxCallBack) {
		AjaxParams par = new AjaxParams();
		par.put("userId", id);
		FinalHttp fh = new FinalHttp();
		fh.post(WebServicUrl.user_view, par, ajaxCallBack);
	}
	public void getData(String uri,Integer page,AjaxParams par, AjaxCallBack<String> ajaxCallBack) {
		FinalHttp fh = new FinalHttp();
		if (page != null&&par!=null) {
			par.put("pageNo", String.valueOf(page));
		}
		fh.post(GlobalVariable.getInterfaceURLPrefix()+uri, par, ajaxCallBack);
	}

	/**
	 * get the carer that going 4s shop
	 * 
	 * @param id
	 * @param ajaxCallBack
	 */
	public void getInshopCarer(Integer page, AjaxCallBack<String> ajaxCallBack) {
		AjaxParams par = new AjaxParams();
		if (page != null) {
			par.put("pageNo", String.valueOf(page));
		}
		FinalHttp fh = new FinalHttp();
		fh.post(WebServicUrl.user_inshopuser, par, ajaxCallBack);
	}

	public void getCarList(Integer page, AjaxCallBack<String> ajaxCallBack) {
		AjaxParams par = new AjaxParams();
		if (page != null) {
			par.put("pageNo", String.valueOf(page));
		}
		FinalHttp fh = new FinalHttp();
		fh.post(WebServicUrl.car_list, par, ajaxCallBack);
	}
	public void getCarListByBrand(Integer page,String companyId,AjaxCallBack<String> ajaxCallBack) {
//		public void getCarListByBrand(Integer page,String brand,AjaxCallBack<String> ajaxCallBack) {
		AjaxParams par = new AjaxParams();
		if (page != null) {
			par.put("pageNo", String.valueOf(page));
		}
//		par.put("brand", brand);
		par.put("companyId", companyId);
		FinalHttp fh = new FinalHttp();
		fh.post(WebServicUrl.car_list_by_brand, par, ajaxCallBack);
	}

	/**
	 * 添加/更新账号
	 * 
	 * user_save </p> identifier 用户识别码 companyId 所属机构ID userName 用户名 phyName
	 * 身份证号 age 年龄 tel 联系电话 sex 性别 plate 车牌号 birthday 生日 email 邮箱 carId 车型ID
	 * maintainMileage 保养里程 maintainTime 保养时间 mileage 行驶里程 buycartime 购车时间
	 * insuCom 保险公司 insuTime 保险时间 inspTime 年检时间 nextUserName 第二联系人名字 nextTel
	 * 第二联系人电话 userId</p> 客户ID(可选，填写默认进入更新模式) </p> 成功 {} 失败
	 * {"code":500,"reason":"系统异常！"}</p>
	 * 
	 * @param userId
	 * @param identifier
	 * @param userName
	 * @param phyName
	 * @param age
	 * @param tel
	 * @param sex
	 * @param plate
	 * @param maintainMileage
	 * @param maintainTime
	 * @param mileage
	 * @param buycartime
	 * @param instCom 保险公司
	 * @param insuTime 保险时间
	 * @param inspTime 年检时间
	 * @param nextUserName
	 * @param nextTel
	 * @param ajaxCallBack
	 */
	public void updateCarerInfo(Integer userId, String identifier,
			String userName, String phyName, String age, String tel,
			String sex, String plate, String carId, String birthday,
			String email, String maintainMileage, String maintainTime,
			String mileage, String buycartime, String insuCom, String insuTime,
			String inspTime, String nextUserName, String nextTel,
			String address, AjaxCallBack<String> ajaxCallBack) {

		AjaxParams par = new AjaxParams();
		par.put("identifier", identifier);
		par.put("userName", userName);
		par.put("phyName", phyName);
		par.put("age", age);
		par.put("tel", tel);
		par.put("sex", sex);
		par.put("plate", plate);
		par.put("carId", carId);
		par.put("birthday", birthday);
		par.put("email", email);
		par.put("maintainMileage", maintainMileage);
		par.put("maintainTime", maintainTime);
		par.put("mileage", mileage);
		par.put("buycartime", buycartime);
		par.put("insuTime", insuTime);
		par.put("insuCom", insuCom);
		par.put("inspTime", inspTime);
		par.put("nextUserName", nextUserName);
		par.put("nextTel", nextTel);
		par.put("address", address);
		par.put("companyId", GlobalVariable.getGlobalInstance().intent
				.getStringExtra(IntentExtraDataKey.COMPANYID));
		if (userId != null) {
			try {
				par.put("userId", String.valueOf(userId));
			} catch (Exception e) {
				lg.e("DataSources", "userId:" + userId
						+ "can not cast this int");
				e.printStackTrace();
			}

		}

		FinalHttp fh = new FinalHttp();
		fh.post(WebServicUrl.user_save, par, ajaxCallBack);

	}

	/**
	 * 保存客户的保养记录 userId recordId plate maintainMileage maintainTime isnormal
	 * currentMileage recordTime remark 客户Id 记录Id（为null为保存） 车牌 保养里程 保养时间
	 * 是否正常保养（1是0否） 当前里程 记录时间 备注
	 */
	public void updateProject(String userId, String plate,
			String maintainMileage, String maintainTime, String currentMileage,
			AjaxCallBack<String> ajaxCallBack) {

		AjaxParams par = new AjaxParams();
		par.put("userId", userId);
		par.put("plate", plate);
		par.put("maintainMileage", maintainMileage);
		par.put("maintainTime", maintainTime);
		par.put("currentMileage", currentMileage);
		par.put("isnormal", "0");
		par.put("remark", "--from Android");

		// yyyy-MM-dd HH:mm:ss
		par.put("recordTime", Dateutil.getTimeString(
				Dateutil.SimpleDateFormat__YYYYMMDDHHMMSS,
				System.currentTimeMillis()));
		FinalHttp fh = new FinalHttp();
		fh.post(WebServicUrl.user_maintain_record_save, par, ajaxCallBack);

	}

	public void updateProject(String userId, String plate,
			String maintainMileage, String maintainTime, String currentMileage,
			String recordTime, int isnormal, AjaxCallBack<String> ajaxCallBack) {

		AjaxParams par = new AjaxParams();
		par.put("userId", userId);
		par.put("plate", plate);
		par.put("maintainMileage", maintainMileage);
		par.put("maintainTime", maintainTime);
		par.put("currentMileage", currentMileage);
		par.put("currentMileage", currentMileage);
		par.put("recordTime", recordTime);
		par.put("isnormal", String.valueOf(isnormal));
		par.put("remark", "--from Android");

		// yyyy-MM-dd HH:mm:ss
		par.put("recordTime", Dateutil.getTimeString(
				Dateutil.SimpleDateFormat__YYYYMMDDHHMMSS,
				System.currentTimeMillis()));
		FinalHttp fh = new FinalHttp();
		fh.post(WebServicUrl.user_maintain_record_save, par, ajaxCallBack);

	}

	/**
	 * 
	 * reserve_list userId 预约客户id ifReply 是否回复(0：未回复，1：已回复) state 状态：0未确认，1已确认
	 * appTime 预约登记日期 companyId 公司ID </p> if clientId is null return all
	 * list,</p> if compantId is null r,the server get it from session</p>
	 * 
	 * @param ajaxCallBack
	 */
	public void getOrderCarerList(Integer page, String clientId,
			String appTime, String isReply, String state, String companyId,
			String pageNo, AjaxCallBack<String> ajaxCallBack) {
		AjaxParams par = new AjaxParams();
		if (!TextUtils.isEmpty(clientId)) {
			par.put("userId", clientId);
		}
		if (!TextUtils.isEmpty(pageNo)) {
			par.put("pageNo", pageNo);
		}
		if (page != null) {
			par.put("pageNo", String.valueOf(page));
		}
		if (!TextUtils.isEmpty(appTime)) {
			par.put("appTime", appTime);
		}
		if (TextUtils.isEmpty(companyId)) {
			companyId = GlobalVariable.getGlobalInstance().intent
					.getStringExtra(IntentExtraDataKey.COMPANYID);
		}
		if (!TextUtils.isEmpty(isReply)) {
			par.put("ifReply", isReply);
		}
		if (!TextUtils.isEmpty(state)) {
			par.put("state", state);
		}
		if (page != null) {
			par.put("pageNo", String.valueOf(page));
		}
		par.put("companyId", companyId);
		FinalHttp fh = new FinalHttp();
		fh.post(WebServicUrl.user_reserve_list, par, ajaxCallBack);

	}

	public void getTodayOrderCarerList(Integer page, String clientId,
			String appTime, String isReply, String state, String companyId,
			String pageNo, AjaxCallBack<String> ajaxCallBack) {
		AjaxParams par = new AjaxParams();
		if (!TextUtils.isEmpty(clientId)) {
			par.put("userId", clientId);
		}
		if (!TextUtils.isEmpty(pageNo)) {
			par.put("pageNo", pageNo);
		}
		if (!TextUtils.isEmpty(appTime)) {
			par.put("appTime", appTime);
		}
		if (TextUtils.isEmpty(companyId)) {
			companyId = GlobalVariable.getGlobalInstance().intent
					.getStringExtra(IntentExtraDataKey.COMPANYID);
		}
		if (!TextUtils.isEmpty(isReply)) {
			par.put("ifReply", isReply);
		}
		if (!TextUtils.isEmpty(state)) {
			par.put("state", state);
		}
		if (page != null) {
			par.put("pageNo", String.valueOf(page));
		}
		par.put("companyId", companyId);
		FinalHttp fh = new FinalHttp();
		fh.post(WebServicUrl.reserve_today_list, par, ajaxCallBack);

	}

	/**
	 * complaint_list userId 投诉客户id ifReply 是否回复(0：未回复，1：已回复) state 状态：0未确认，1已确认
	 * appTime 投诉登记日期 companyId 公司ID</p> if clientId is null return all
	 * list,</p> if compantId is null r,the server get it from session</p>
	 * 
	 * @param clientId
	 * @param appTime
	 * @param companyId
	 * @param ajaxCallBack
	 */
	public void getComplaintList(Integer page, String clientId, String appTime,
			String pageNo, String isReply, String state, String companyId,
			AjaxCallBack<String> ajaxCallBack) {
		AjaxParams par = new AjaxParams();
		if (!TextUtils.isEmpty(pageNo)) {
			par.put("pageNo", pageNo);
		}
		if (page != null) {
			par.put("pageNo", String.valueOf(page));
		}
		if (!TextUtils.isEmpty(clientId)) {
			par.put("userId", clientId);
		}
		if (!TextUtils.isEmpty(appTime)) {
			par.put("appTime", appTime);
		}
		if (TextUtils.isEmpty(companyId)) {
			companyId = GlobalVariable.getGlobalInstance().intent
					.getStringExtra(IntentExtraDataKey.COMPANYID);
		}
		if (!TextUtils.isEmpty(isReply)) {
			par.put("ifReply", isReply);
		}
		if (!TextUtils.isEmpty(state)) {
			par.put("state", state);
		}
		if (page != null) {
			par.put("pageNo", String.valueOf(page));
		}
		par.put("companyId", companyId);
		FinalHttp fh = new FinalHttp();
		fh.post(WebServicUrl.user_complaint_list, par, ajaxCallBack);
	}

	/**
	 * 
	 * @param id
	 * @param ajaxCallBack
	 */
	public void getReserveReplyList(Integer page, int id,
			AjaxCallBack<String> ajaxCallBack) {
		AjaxParams par = new AjaxParams();
		par.put("reserveId", String.valueOf(id));
		if (page != null) {
			par.put("pageNo", String.valueOf(page));
		}
		FinalHttp fh = new FinalHttp();
		fh.post(WebServicUrl.user_reserve_reply_list, par, ajaxCallBack);
	}

	/**
	 * 
	 * @param id
	 * @param ajaxCallBack
	 */
	public void getComplaintReplyList(Integer page, int id,
			AjaxCallBack<String> ajaxCallBack) {
		AjaxParams par = new AjaxParams();
		par.put("complaintId", String.valueOf(id));
		if (page != null) {
			par.put("pageNo", String.valueOf(page));
		}
		FinalHttp fh = new FinalHttp();
		fh.post(WebServicUrl.user_complaint_reply_list, par, ajaxCallBack);
	}

	public void getReminder(Integer page, int id,
			AjaxCallBack<String> ajaxCallBack) {
		AjaxParams par = new AjaxParams();
		par.put("accountId", String.valueOf(id));
		if (page != null) {
			par.put("pageNo", String.valueOf(page));
		}
		FinalHttp fh = new FinalHttp();
		fh.post(WebServicUrl.reserve_today_amount, par, ajaxCallBack);
	}

	public final static String BusinessType_canshuyichang = "warn_list";
	public final static String BusinessType_pengzhuangtixing = "hit_list";
	public final static String BusinessType_kehuliushi = "competition_recorder_page";
	public final static String BusinessType_kehuduandian = "user_dispower";
	public final static String BusinessType_cheliangguzhang = "fault";
	public final static String BusinessType_examined = "examined";
	public final static String BusinessType_maintained = "maintained";
	public final static String BusinessType_renewal = "renewal";
	public final static String BusinessType_replaceCar = "replaceCar";
	public final static String BusinessType_firstmaintained = "firstmaintained";
	public final static String BusinessType_guarantee = "guarantee";
	public final static String BusinessType_fault = "fault";
	public final static String BusinessType_birthday = "birthday";

	/**
	 * 获取商机
	 * 
	 * @param page
	 * @param business
	 *            examined/maintained/renewal/replaceCar/firstmaintained/
	 *            guarantee/fault[新增故障车辆]
	 * @param ajaxCallBack
	 */
	public void getBusiness(Integer page, String business,
			AjaxCallBack<String> ajaxCallBack) {
		AjaxParams par = new AjaxParams();
		if (page != null) {
			par.put("pageNo", String.valueOf(page));
		}
		if (business != null && business.equals("user_business")) {
			par.put("business", "fault");
		}
		par.put("business", business);
		FinalHttp fh = new FinalHttp();
		fh.post(WebServicUrl.user_business, par, ajaxCallBack);
	}

	/**
	 * creator 发送人 messageType 消息类型 （0：单发[]，1：选择发送，2：自动发送） createTime 发生日期
	 * companyId 公司ID
	 * 
	 * @param ajaxCallBack
	 */
	public void getmessageList(Integer page, String creator,
			String messageType, String createTime, String companyId,
			String identifier, String pageNo, AjaxCallBack<String> ajaxCallBack) {
		AjaxParams par = new AjaxParams();
		if (!TextUtils.isEmpty(pageNo)) {
			par.put("pageNo", pageNo);
		}
		if (!TextUtils.isEmpty(creator)) {
			par.put("creator", creator);
		}
		if (!TextUtils.isEmpty(messageType)) {
			par.put("messageType", messageType);
		}
		if (page != null) {
			par.put("pageNo", String.valueOf(page));
		}
		if (identifier != null) {
			par.put("identifier", identifier);
		}
		if (TextUtils.isEmpty(companyId)) {
			companyId = GlobalVariable.getGlobalInstance().intent
					.getStringExtra(IntentExtraDataKey.COMPANYID);
		}
		if (!TextUtils.isEmpty(createTime)) {
			par.put("createTime", createTime);
		}
		par.put("companyId", companyId);
		FinalHttp fh = new FinalHttp();
		fh.post(WebServicUrl.user_message_list, par, ajaxCallBack);
	}

	/**
	 * 得到用户当前的GPS位置坐标
	 * 
	 * @param identifier
	 * @param ajaxCallBack
	 */
	public void getCarerGpsInfo(String identifier,
			AjaxCallBack<String> ajaxCallBack) {
		AjaxParams par = new AjaxParams();
		par.put("identifier", identifier);
		FinalHttp fh = new FinalHttp();
		fh.post(WebServicUrl.user_showpoint, par, ajaxCallBack);
	}

	/**
	 * 单用户发送消息 content 消息内容 messageTitle 标题 messageSummary 简介 messageImage
	 * 缩略图网络地址 searchCode 查询客户的json格式条件 identifier 接收的客户识别码，单个客户发送适用
	 * 
	 * @param content
	 * @param messageTitle
	 * @param messageSummary
	 * @param identifier
	 * @param searchCode
	 * @param ajaxCallBack
	 */
	public void sendMessage(String content, String messageTitle,
			String messageSummary, String messageImage, String identifier,
			String searchCode, AjaxCallBack<String> ajaxCallBack) {
		AjaxParams par = new AjaxParams();
		par.put("content", content);
		par.put("messageTitle", messageTitle);
		par.put("messageSummary", messageSummary);
		par.put("messageImage", messageImage);
		par.put("searchCode", searchCode);
		par.put("identifier", identifier);
		FinalHttp fh = new FinalHttp();
		fh.post(WebServicUrl.message_send, par, ajaxCallBack);
	}

	/**
	 * 调用API去调百度的导航仪坐标转换接口
	 * 
	 * @param x
	 * @param y
	 * @param ajaxCallBack
	 */
	public void converGPS(double x, double y, AjaxCallBack<String> ajaxCallBack) {
		FinalHttp fh = new FinalHttp();
		lg.i(TAG,
				WebServicUrl.getURL_GPS_Conversion(String.valueOf(x),
						String.valueOf(y)));
		fh.get(WebServicUrl.getURL_GPS_Conversion(String.valueOf(x),
				String.valueOf(y)), ajaxCallBack);
	}

	/**
	 * 获取行车数据
	 * 
	 * @param fromDate
	 * @param toDate
	 * @param identifier
	 * @param ajaxCallBack
	 */
	public void getCarerRecord(String fromDate, String toDate,
			String identifier, AjaxCallBack<String> ajaxCallBack) {
		FinalHttp fh = new FinalHttp();
		AjaxParams par = new AjaxParams();
		par.put("fromDate", fromDate);
		par.put("toDate", toDate);
		par.put("identifier", identifier);
		fh.post(WebServicUrl.user_recorder, par, ajaxCallBack);
	}

	/**
	 * 异常参数列表
	 * 
	 * @param _page
	 * @param ajaxCallBackProxy
	 */
	public void getCarException(Integer _page,
			AjaxCallBackProxy<String> ajaxCallBackProxy) {
		FinalHttp fh = new FinalHttp();
		AjaxParams par = new AjaxParams();
		fh.post(WebServicUrl.warn_list, par, ajaxCallBackProxy);
	}

	/**
	 * 异常参数历史列表
	 * 
	 * @param _page
	 * @param ajaxCallBackProxy
	 */
	public void getCarExceptionHistoryList(Integer _page, String userId,
			AjaxCallBackProxy<String> ajaxCallBackProxy) {
		FinalHttp fh = new FinalHttp();
		AjaxParams par = new AjaxParams();
		par.put("userId", userId);
		fh.post(WebServicUrl.warn_detail, par, ajaxCallBackProxy);
	}

	/**
	 * 客户流失
	 * 
	 * @param _page
	 * @param ajaxCallBackProxy
	 */
	public void getCarerMiss(Integer _page,
			AjaxCallBackProxy<String> ajaxCallBackProxy) {
		FinalHttp fh = new FinalHttp();
		AjaxParams par = new AjaxParams();
		fh.post(WebServicUrl.competition_recorder_page, par, ajaxCallBackProxy);
	}

	/**
	 * 统计报表
	 * 
	 * @param _page
	 * @param type
	 * @param year
	 *            按月
	 * @param month
	 *            按日
	 * @param ajaxCallBackProxy
	 */
	private void getReport(Integer _page, String type, String year,
			String month, AjaxCallBackProxy<String> ajaxCallBackProxy) {
		FinalHttp fh = new FinalHttp();
		AjaxParams par = new AjaxParams();
		par.put("type", type);
		par.put("year", year);
		par.put("month", month);
		fh.post(WebServicUrl.stat_report, par, ajaxCallBackProxy);
	}

	public void getReportByYear(Integer _page, String type,
			AjaxCallBackProxy<String> ajaxCallBackProxy) {
		getReport(_page, type, null, null, ajaxCallBackProxy);
	}

	public void getReportByMonth(Integer _page, String type, String year,
			AjaxCallBackProxy<String> ajaxCallBackProxy) {
		getReport(_page, type, year, null, ajaxCallBackProxy);
	}

	public synchronized void getReportByDay(Integer _page, String type, String year,
			String month, AjaxCallBackProxy<String> ajaxCallBackProxy) {
		getReport(_page, type, year, month, ajaxCallBackProxy);
	}

}
